package sc

import java.sql.DriverManager
import scala.io.Source

object ToMysql {
  def main(args: Array[String]): Unit = {
    Class.forName("com.mysql.cj.jdbc.Driver")
    var conn = DriverManager.getConnection("jdbc:mysql://192.168.205.129:3306/students?useSSL=false&serverTimezone=UTC&characterEncoding=UTF-8", "root", "123456")
    var source = Source.fromFile("scala/data/students.csv")
    var list = source.getLines()
    for (line <- list) {
      var sp: Array[String] = line.split(",")
//      var id = sp(0)
//      var name = sp(1)
//      var age = sp(2)
//      var gender = sp(3)
//      var clazz =sp(4)
//      var prep = conn.prepareStatement("insert into students values (?,?,?,?,?)")
//      prep.setInt(1, id.toInt)
//      prep.setString(2, name)
//      prep.setInt(3, age.toInt)
//      prep.setString(4, gender)
//      prep.setString(5, clazz)
      val prep = conn.prepareStatement("insert into students values (?,?,?,?,?)")
      prep.setInt(1, sp(0).toInt)
      prep.setString(2,sp(1))
      prep.setInt(3, sp(2).toInt)
      prep.setString(4, sp(3))
      prep.setString(5, sp(4))
      prep.execute()
    }
    conn.close()
  }

}
